﻿
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using sp = MyData.SharePoint;
using MyData.ExpressionBuilder.Model;
using Microsoft.SharePoint.Client;
using MyData.Activities.SharePointCSOM.Caml;
using System.Xml.Linq;

using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.Office.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using System.Net;

namespace MyDataTestProject
{

  [TestClass]
  public class SharePoint
  {

    internal class CamlActivity
    {
      public string ComparisonOperator { get; set; }
      public string FieldRef { get; set; }
      public string ValueType { get; set; }
      public object Value { get; set; }
    }

    [TestMethod]
    public void Fields()
    {
      sp.SharePointItemFields fields = new sp.SharePointItemFields("http://localhost", "Tasks");

      Assert.IsTrue(fields.Items.Count > 0);
    }

    [TestMethod]
    public void MapListItems()
    {
      string url = "http://dvana-pc/Test";
      string title = "Employee";

      sp.SharePointItemFields fields = new sp.SharePointItemFields(url, title);
      var items = fields.Items;
    }

    [TestMethod]
    public void ContentTypes()
    {
      string InArgument_Url = "http://dvana-pc/Test";
      string InArgument_Title = "Employee";

      List<MyData.ExpressionBuilder.Model.ListItem> listFields = new List<MyData.ExpressionBuilder.Model.ListItem>();

      using (ClientContext clientContext = new ClientContext(InArgument_Url))
      {
        if (InArgument_Title != null)
        {
          List list = clientContext.Web.Lists.GetByTitle(InArgument_Title);

          CamlQuery camlQuery = new CamlQuery();
          //How to OrderBy on server?
          camlQuery.ViewXml = "<View/>";

          ListItemCollection listItems = list.GetItems(camlQuery);
          clientContext.Load(list);

          clientContext.Load(list.ContentTypes);
          clientContext.ExecuteQuery();

          IEnumerable<Microsoft.SharePoint.Client.Field> fields = clientContext.LoadQuery(
                 list.Fields.Include(f => f.InternalName).Where(f => f.Hidden != true));
          clientContext.ExecuteQuery();

          IEnumerable<Microsoft.SharePoint.Client.Field> sortedFields = from f in fields orderby f.InternalName select f;

          foreach (Microsoft.SharePoint.Client.Field f in sortedFields)
          {
            listFields.Add(new MyData.ExpressionBuilder.Model.ListItem(f.InternalName));
          }
        }
      }

    }

    [TestMethod]
    public void CamlBuilder()
    {
      List<CamlActivity> camlActivities = new List<CamlActivity> 
      { new CamlActivity { ComparisonOperator = "Eq", FieldRef = "EmployeeID", Value=1, ValueType="Number" },
        new CamlActivity { ComparisonOperator = "Eq", FieldRef = "Title", Value="SDE", ValueType="Text" }
      };

      List<string> viewFields = new List<string> { "EmployeeID", "Title" };

      var xdoc = new XElement(CamlElements.View, 
              new XElement(CamlElements.Query,
                 camlActivities
                .Select(activity => CamlElements.Where(activity.ComparisonOperator, activity.FieldRef, activity.ValueType, activity.Value))),
              new XElement(CamlElements.ViewFields,
                viewFields
                .Select(name => CamlElements.ViewFieldNames(name))),
              new XElement(CamlElements.RowLimit, 100)); 
    }

    [TestMethod]
    public void SimpleCAMLWhere()
    {

      String siteUrl = "http://dvana-pc/Test";

      ClientContext clientContext = new ClientContext(siteUrl);

      List list = clientContext.Web.Lists
          .GetByTitle("Employee");
      CamlQuery camlQuery = new CamlQuery();
 
      camlQuery.ViewXml =
      @"<View> 
        <Query> 
          <Where> 
            <Eq> 
              <FieldRef Name='EmployeeID'/> 
              <Value Type='Text'>1</Value> 
            </Eq> 
          </Where>          
        </Query>
         
        <ViewFields> 
          <FieldRef Name='EmployeeID'/> 
          <FieldRef Name='Title'/> 
        </ViewFields> 
        <RowLimit>100</RowLimit>
      </View>";

      ListItemCollection listItems = list.GetItems(camlQuery);
      clientContext.Load(listItems);

      clientContext.ExecuteQuery();

      Assert.IsTrue(listItems.Count > 0);

    }
  }
}


